import { Image } from "antd";
import React, { useState, useEffect } from "react";
import { downloadFileFromOSS } from "./client/AliOss";

/**
 * @description 阿里云图片预览 简单下载模式
 * @returns 
 */
const AliOssViewImage = ({
    fileName,
    width = 100,
    preview = false,
}) => {
    const [src, setSrc] = useState("error");
    const [loading, setLoading] = useState(false);
    // initImage
    const getImage = async (fileName, callBack = Function) => {
        let metaData = {
            fileName: fileName,
            busiId: `contractEntity.contractCode`,
            busiType: 'OBC'
        }
        setLoading(true);
        let res = await downloadFileFromOSS(metaData);
        if (res.status === "0000") {
            callBack(res?.data?.url)
            // setFileList([{
            //     url: res?.data?.url,
            //     uid: uuid(),
            //     size: res?.data?.size
            // }])
        } else {
            callBack("error")
        }
        setLoading(false)
    }

    useEffect(() => {
        getImage(fileName, setSrc)
    }, [fileName])

    return (
        <>
            <Image
                width={width}
                preview={preview}
                loading={loading}
                src={src}
                fallback=""

            />
        </>
    )

}
export default AliOssViewImage;